/* global window */
import modelExtend from 'dva-model-extend'
import { create, remove, update,query } from './services/highQualityChatRecord'
import * as deptService from './services/highQualityChatRecords'
import * as employeeService from '../employee/services/employees'
import {message} from 'antd'
import { pageModel } from 'utils/model'
import { queryArray } from 'utils'

// const { query } = deptService

// const setParent = (list) => {
//   let parent
//   list.map(p => {
//     if (p.parentId && p.parentId !== (parent ? parent.id : -1)) {
//       parent = queryArray(list, p.parentId, 'id')
//     }

//     if (p.parentId && p.parentId === parent.id) {
//       p['parent'] = {
//         id: parent.id,
//         sn: parent.sn,
//         name: parent.name,
//       }
//     }
//   })
//   list.reverse()
// }

export default modelExtend(pageModel, {
  namespace: 'highQualityChatRecord',

  state: {
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    empDataSource: [],
    activeKey: "group_list",
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/highQualityChatRecord') {
          const payload = location.query || { current: 1, size: 10 }
          dispatch({
            type: 'query',
            payload,
          })
          dispatch({
            type: 'searchEmp',
            payload: { key: "any" },
          })
        }
      })
    },
  },

  effects: {

    * query({ payload = {} }, { call, put }) {
      const response = yield call(query, payload)
      const { data } = response
      if (response) {
        // setParent(data.records)
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
          },
        })
      }
    },

    * searchEmp({ payload = {} }, { call, put }) {
      const response = yield call(employeeService.searchEmp, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            empDataSource: data,
          },
        })
      }
    },

    * delete({ payload }, { call, put, select }) {
      const data = yield call(remove, payload)
      const { selectedRowKeys } = yield select(_ => _.highQualityChatRecord)
      if (data.code === 0) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
      } else {
        throw data
      }
    },

    * multiDelete({ payload }, { call, put }) {
      const data = yield call(deptService.remove, payload)
      if (data.code === 0) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
      } else {
        throw data
      }
    },

    * create({ payload }, { call, put }) {
      const {code, msg} = yield call(create, payload)
      if (code === 0) {
        yield put({ type: 'hideModal' })
        message.success(msg)
      } else {
        message.error(msg)
      }
    },

    * update({ payload }, { select, call, put }) {
      const id = yield select(({ highQualityChatRecord }) => {
        if (payload.id) {
          return payload.id
        }
        return highQualityChatRecord.currentItem.id
      })

      const newDept = { ...payload, id }
      const data = yield call(update, newDept)
      if (data.code === 0) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },

  },

  reducers: {

    showModal(state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal(state) {
      return { ...state, modalVisible: false }
    },

  },
})
